window.addEventListener("load", () => {

      $(function () {
            class Register {
                  constructor() {
                        console.log(111);
                        this.btn = $("#btn_submit");
                        this.username = $("#account");
                        this.nickname = $("#nickname");
                        this.password = $("#password");
                        this.rpassword = $("#repassword");
                        this.check_box = $("#remember");
                        // 提示框 ; 
                        this.alert = $("#register-alert");
                        this.bindEvent();
                  }

                  bindEvent() {
                        this.btn.on("click", () => {
                              if(this.check_box.is(":checked")){
                                    this.register();
                              }else{
                                    alert("请阅读并同意用户须知！")
                              }
                        });
                  }
                  register() {
                        var name = this.username.val();
                        var psd = this.password.val();
                        var rpsd = this.rpassword.val();
                        var nick = this.nickname.val();
                        axios.post("http://127.0.0.1:8888/users/register", `username=${name}&rpassword=${rpsd}&nickname=${nick}&password=${psd}`)
                              .then((res) => {
                                    console.log(res);
                                    switch (res.data.code) {
                                          case 1:
                                                this.success();
                                                break;
                                          default:
                                                this.error(res.data);
                                                break;
                                    }
                              });

                  }
                  error(res) {
                        this.alert.removeClass("alert-success alert-danger");
                        this.alert.addClass("alert-danger");
                        this.alert.html("抱歉注册失败 : " + res.message);

                        setTimeout(() => {
                              this.alert.removeClass("alert-success alert-danger");
                        }, 2000)
                  }
                  success() {
                        this.alert.removeClass("alert-success alert-danger");
                        this.alert.addClass("alert-success");
                        this.alert.html("恭喜注册成功稍后我们将为您跳转页面");

                        setTimeout(() => {
                              location.href = "./login.html";

                              sessionStorage.setItem("usermsg", JSON.stringify({
                                    username: this.username.val(),
                                    password: this.password.val()
                              }));
                        }, 2000)
                  }
            }
            let r = new Register()
      })

});